// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Follow me - action script for AI follow players
// -----------------------------------------------

private ["_unit","_leader","_ingroup","_group","_building"];

_unit   = _this select 0;
_leader = _this select 1;

//A3 TODO: setVehicleInit
_unit setVehicleInit "this enableAI 'Move'; this stop false;";
processInitCommands;

_unit doWatch position _leader;

if !(leader _leader == _leader) then {
	_group = createGroup west;
	[_leader] joinSilent _group;
} else {
	_group = group _leader;
};

[_unit] joinSilent _group;
_group selectLeader _leader;
_unit setVariable ["wchostage", false, true];

_ingroup = true;
while {_ingroup} do {
	// Can not bring weapon
	if (primaryWeapon _unit != "") then {
		_unit playMove "AinvPknlMstpSlayWrflDnon_medic";
		sleep 4;
		removeAllWeapons _unit;
	};
	if (count (units(group _unit)) > 1) then {_ingroup = true} else {_ingroup = false};
	if (vehicle _unit == _unit) then {
		if (_unit distance _leader < 150) then {
			_unit doFollow _leader;
		} else {
			doStop _unit;
		};
		if (_unit distance _leader > 15) then {
			_building = nearestObjects [_unit, ["House"], 10];
			if (count _building > 0) then {
				if ((getPosATL _leader) distance _unit < 300) then {
					_unit setPosATL getPosATL _leader;
				};
			};
		};
	};
	sleep 10;
};